package com.qs.controller;


import com.qs.annotation.OperationLog;
import com.qs.pojo.dto.GoodsDto;
import com.qs.pojo.dto.GoodsIndexVo;
import com.qs.pojo.entity.Brand;
import com.qs.pojo.entity.Goods;
import com.qs.pojo.vo.GoodsVo;
import com.qs.response.Response;
import com.qs.service.IBrandService;
import com.qs.service.IGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-11-14
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController {

    private final IGoodsService goodsService;

    private final IBrandService brandService;

    /**
     * 后台商户获取商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/list/{pageNo}/{pageSize}/{storeId}")
    public Response list(@PathVariable Integer pageNo,
                         @PathVariable Integer pageSize,
                         @PathVariable Integer storeId) {
        log.info("后台商户获取商品数据请求进来--------------------");
        //TODO: 所有的redis缓存还未解决缓存雪崩等问题，
        // 后期优化时需要给不同的缓存key的TTL添加随机值
        Map<String,Object> goodsMap = goodsService.getList(pageNo,pageSize,storeId);
        return Response.success(goodsMap);
    }

    /**
     * 前台订单页商户获取商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/get/goodId/{ids}/{orderId}")
    public Response getGoodsList(@PathVariable("ids") List<Integer> ids,
                                 @PathVariable("orderId") String orderId) {
        log.info("前台订单页商户获取商品数据请求进来--------------------");
        Map<String,Object> goodsList = goodsService.getGoodsList(ids,orderId);
        return Response.success(goodsList);
    }

    /**
     * 后台管理员获取所有商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/backend/list/{pageNo}/{pageSize}")
    public Response backendGoodsList(@PathVariable("pageNo") Integer pageNo,
                              @PathVariable("pageSize") Integer pageSize) throws IOException {
        log.info("后台管理员获取所有商品数据请求进来--------------------");
        Map<String,Object>  goodsMap = goodsService.backendGoodsList(pageNo,pageSize);
        return Response.success(goodsMap);
    }

    /**
     * 前台获取热卖商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/front/hot/list/{pageNo}/{pageSize}")
    public Response frontHotGoodsList(@PathVariable("pageNo") Integer pageNo,
                              @PathVariable("pageSize") Integer pageSize) throws IOException {
        log.info("前台获取热卖商品数据请求进来--------------------");
        List<GoodsDto>  goodsList = goodsService.frontHotGoodsList(pageNo,pageSize);
        return Response.success(goodsList);
    }

    /**
     * 前台获取推荐商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/front/recommend/list/{pageNo}/{pageSize}")
    public Response frontRecommendGoodsList(@PathVariable("pageNo") Integer pageNo,
                                      @PathVariable("pageSize") Integer pageSize) throws IOException {
        log.info("前台获取推荐商品数据请求进来--------------------");
        List<GoodsDto>  goodsList = goodsService.frontRecommendGoodsList(pageNo,pageSize);
        return Response.success(goodsList);
    }

    /**
     * 前台商品搜索结果返回接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/search/goods/front/{pageNo}/{pageSize}/{ids}/{msg}")
    public Response searchGoodsFrontList(@PathVariable("pageNo") Integer pageNo,
                                    @PathVariable("pageSize") Integer pageSize,
                                    @PathVariable("ids") List<Integer> ids,
                                    @PathVariable("msg") String msg) throws IOException {
        log.info("前台商品搜索结果返回接口请求进来--------------------");
        Map<String,Object>  searchGoods = goodsService.searchFrontList(pageNo,pageSize,ids,msg);
        return Response.success(searchGoods);
    }

    /**
     * 前台搜索接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/search/{param}/{selectMsg}")
    public Response frontSearchList(@PathVariable("param") String param,
                                    @PathVariable("selectMsg") String selectMsg) throws IOException {
        log.info("前台搜索接口请求进来--------------------");
        List<Integer> goodsIds = goodsService.frontSearchList(param,selectMsg);
        return Response.success(goodsIds);
    }

    /**
     * 前台获取用户点击某分类下的商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/category/list/{pageNo}/{pageSize}/{categoryName}")
    public Response getCategoryGoods(@PathVariable("pageNo") Integer pageNo,
                                     @PathVariable("pageSize") Integer pageSize,
                                     @PathVariable("categoryName") String categoryName) {
        log.info("前台获取用户点击某分类下的商品数据请求进来--------------------");
        Map<String,Object> goodsMap = goodsService.getCategoryGoods(pageNo,pageSize,categoryName);
        return Response.success(goodsMap);
    }

    /**
     * 前台获取猜你喜欢商品数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/like/list/{pageNo}/{pageSize}")
    public Response getLikeGoods(@PathVariable("pageNo") Integer pageNo,
                                     @PathVariable("pageSize") Integer pageSize) {
        log.info("前台获取猜你喜欢商品数据请求进来--------------------");
        List<GoodsDto> goodsDtoList = goodsService.getLikeGoods(pageNo,pageSize);
        return Response.success(goodsDtoList);
    }

    /**
     * 获取商品详情数据
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/detail/{id}")
    public Response getDetail(@PathVariable Integer id) {
        log.info("获取商品详情数据请求进来--------------------");
        //TODO: 后期优化时需添加到缓存中
        GoodsDto goodsDto = goodsService.getDetail(id);
        return Response.success(goodsDto);
    }

    /**
     * 获取商品品牌接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/brandList")
    public Response getBrand() {
        log.info("获取商品品牌接口请求进来--------------------");
        List<Map<String,Object>> brands = brandService.getBrandList();
        return Response.success(brands);
    }

    /**
     * 获取商品二级分类接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/category")
    public Response getCategory() {
        log.info("获取商品二级分类接口请求进来--------------------");
        List<Map<String,Object>> categoryList = brandService.getCategory();
        return Response.success(categoryList);
    }

    /**
     * 获取用户所属的店铺
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/store/{userId}")
    public Response getStores(@PathVariable("userId") Integer userId) {
        log.info("获取用户所属的店铺接口请求进来--------------------");
        List<Map<String,Object>> stores = brandService.getStores(userId);
        return Response.success(stores);
    }

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping("/save")
    @OperationLog(operModul = "商品管理-新增商品",operDesc = "商户新增店铺商品")
    public Response saveGoods(@RequestBody Goods goods) {
        log.info("新增商品请求进来--------------------");
        goodsService.saveGoods(goods);
        return Response.success();
    }

    /**
     * 修改商品
     * @param
     * @param
     * @param
     * @return
     */
    @PutMapping("/update")
    @OperationLog(operModul = "商品管理-修改商品",operDesc = "商户修改店铺商品信息")
    public Response update(@RequestBody GoodsDto goodsDto) {
        log.info("修改商品请求进来--------------------");
        goodsService.updateByKey(goodsDto);
        return Response.success();
    }

    /**
     * 商品数据回显
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/getById/{id}")
    public Response echo(@PathVariable Integer id) {
        log.info("商品数据回显请求进来--------------------");
        GoodsDto goodsDto = goodsService.echo(id);
        return Response.success(goodsDto);
    }

    /**
     * 获取首页的商品轮播图
     * @return
     */
    @GetMapping("/index/image")
    public Response getImage(){
        List<GoodsIndexVo> goodsIndexVos = goodsService.getImage();
        return Response.success( goodsIndexVos);
    }

    /**
     * 删除商品
     * @param
     * @param
     * @param
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    @OperationLog(operModul = "商品管理-删除商品",operDesc = "商户删除店铺商品")
    public Response delete(@PathVariable List<Integer> ids) {
        log.info("删除商品请求进来--------------------");
        goodsService.deleteBatchByIds(ids);
        return Response.success();
    }
}
